package com.lzm.controller;


import com.github.pagehelper.PageInfo;
import com.lzm.pojo.Drug;
import com.lzm.pojo.Hosregister;
import com.lzm.service.drugService;
import com.lzm.vo.QueryVo;
import com.lzm.vo.QueryVo2;
import com.lzm.vo.ResultVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@ResponseBody
@RequestMapping("drug")
public class drugController {

    @Resource
    private drugService drugService;
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVo<Drug> queryByPage(Integer pageNum, Integer pageSize, QueryVo2 vo) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        PageInfo<Drug> drugPageInfo = drugService.queryByPage(pageNum, pageSize, vo);
        return new ResultVo(drugPageInfo);
    }

    @RequestMapping(value = "add",method = RequestMethod.POST)
    public ResultVo<Drug> addDrug(Drug drug,@RequestParam("file") MultipartFile file,HttpServletRequest request){
        //文件上传到指定服务器的位置
        String path = request.getServletContext().getRealPath("/Images/uploadFile/");
        //获取原始文件的名称
        String originalFilename = file.getOriginalFilename();
        //随机生成文件名
        String randomName = UUID.randomUUID().toString().replace("-", "");
        //截取后缀名
        String hz = originalFilename.substring(originalFilename.lastIndexOf("."));
        //生成新的文件名
        String logoName = randomName + hz;
        int num = 0;
        try {
            file.transferTo(new File(path +  logoName));
            System.out.println("上传成功！" + path +  logoName);
            //修改到数据库
            drug.setDrug_id(drug.getDrug_id());
            drug.setDrug_url(logoName);
            drug.setDrug_state(0);
            num = drugService.addDrug(drug);
            if (num == 1) {
                return new ResultVo<Drug>();
            }
            return new ResultVo<>(500, "服务器内部异常，请稍后再试");
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultVo<>(500, "图片上传出现问题！"+e.getMessage());
        }
    }

        @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public ResultVo<Drug> queryById(@PathVariable("id") String drug_id){
        Drug drug = drugService.queryById(drug_id);
        return new ResultVo<>(drug);
    }
    @RequestMapping(value = "{id}",method = RequestMethod.PUT)
    public ResultVo<Drug> updateHosRegister(@PathVariable("id") String drug_id,Drug drug){
        drug.setDrug_id(drug_id);
        int i = drugService.updateDrug(drug);
        if(i==1){
            return new ResultVo<Drug>();
        }
        return new ResultVo<>(500,"更新失败！服务器内部异常，请稍后再试");
    }

    @RequestMapping("/updateM")
    public ResultVo<Drug> updateM(Drug drug,@RequestParam("file") MultipartFile file,HttpServletRequest request){
        //上传
        //文件上传到指定服务器的位置
        String path = request.getServletContext().getRealPath("/Images/uploadFile/");
        //获取原始文件的名称
        String originalFilename = file.getOriginalFilename();
        //随机生成文件名
        String randomName = UUID.randomUUID().toString().replace("-", "");
        //截取后缀名
        String hz = originalFilename.substring(originalFilename.lastIndexOf("."));
        //生成新的文件名
        String logoName = randomName + hz;
        int num = 0;
        try {
            file.transferTo(new File(path +  logoName));
            System.out.println("上传成功！" + path +  logoName);
            //修改到数据库
            drug.setDrug_id(drug.getDrug_id());
            drug.setDrug_url(logoName);
            num = drugService.updateDrug(drug);
            if (num == 1) {
                return new ResultVo<Drug>();
            }
            return new ResultVo<>(500, "服务器内部异常，请稍后再试");
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultVo<>(500, "图片上传出现问题！"+e.getMessage());
        }
    }

}
